package com.zyd.blog.personal.service.impl;

import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.persistence.mapper.UserProductionMapper;
import com.zyd.blog.personal.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 用户作品业务逻辑层
 * @author mfy
 */
@Service
public class UserProductionServiceImpl implements UserProductionService {

    @Resource
    private UserProductionMapper userProductionMapper;

    @Resource
    private UserThumbService userThumbService;

    @Resource
    private UserCommentService userCommentService;

    @Resource
    private UserCollectionService userCollectionService;

    @Resource
    private UserShareService userShareService;

    @Override
    public List<Map<String,Object>> getUserProduction(Long selfId,Long productionId,Long userId,String status,String isDelete){
        List<Map<String,Object>> result = new ArrayList<>();
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("id", productionId);
        condition.put("status", status);
        condition.put("isDelete", isDelete);
        condition.put("userId",userId);
        List<Map<String,Object>> userProduction = userProductionMapper.queryUserProductionByCondition(condition);
        for(Map<String,Object> item : userProduction){
            Map<String,Object> map = new HashMap<>(16);
            Long proId = (Long)item.get("id");
            map.put("productionId",proId);
            map.put("thumbNum",userThumbService.getThumbInfoByCondition(null,proId, Status.STATUS_YES).size());
            map.put("comment",userCommentService.getAllCommentByProductionId(proId));
            map.put("collectionNum",userCollectionService.getCollectionInfo(null,proId,Status.STATUS_YES).size());
            map.put("title",item.get("title"));
            String[] label = ((String)item.get("label")).split("#");
            map.put("label",label);
            map.put("content",item.get("content"));
            String[] coverPicture = ((String)item.get("coverPicture")).split("#");
            map.put("coverPicture",coverPicture);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            map.put("publishTime",simpleDateFormat.format(new Date((Long) item.get("publishTime"))));
            if(selfId != null){
                if(userThumbService.isHaveThumb(null,selfId,proId,Status.STATUS_YES)){
                    map.put("isThumb","Y");
                }else{
                    map.put("isThumb","N");
                }
                if(userShareService.isHaveShare(null,selfId,proId,Status.STATUS_YES)){
                    map.put("isShare","Y");
                }else{
                    map.put("isShare","N");
                }
                if(userCollectionService.isHaveCollection(null,selfId,proId,Status.STATUS_YES)){
                    map.put("isCollection","Y");
                }else{
                    map.put("isCollection","N");
                }
            }
            result.add(map);
        }
        return result;
    }
}
